//
//  HRFollowDynamicLayout.m
//  Hear
//
//  Created by mac on 2017/5/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRFollowDynamicLayout.h"
#import "HRFollowDynamicPhotoView.h"
#import "HRDynamicCommentView.h"
@implementation HRFollowDynamicLayout




-(void)setDynamicModel:(HRDynamicModel *)dynamicModel{
    _dynamicModel = dynamicModel;
    
    CGFloat leftPading = 15;
    
    _iconImageViewFrame = CGRectMake(leftPading, leftPading, 50, 50);
    
    _nickNameLabelFrame = CGRectMake(CGRectGetMaxX(_iconImageViewFrame)+leftPading, _iconImageViewFrame.origin.y+5, kFullWidth-leftPading*3-_iconImageViewFrame.size.width, 25);
    
    
    _timeLabelFrame = CGRectMake(_nickNameLabelFrame.origin.x, CGRectGetMaxY(_nickNameLabelFrame), _nickNameLabelFrame.size.width, 10);
    
    
    _moreButtonFrame = CGRectMake(kFullWidth-60, _timeLabelFrame.origin.y-10, 60, 20);
    
//    CGFloat contentHeight = [[self attStringFromTextForChatting:dynamicModel.MomentNotes textFont:CustomUIFont(13)] boundingRectWithSize:CGSizeMake(kFullWidth-leftPading*2, HUGE) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 8;
    NSDictionary *dic = @{NSFontAttributeName:CustomUIFont(14), NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f
                          };
    
    CGFloat contentWidht = [dynamicModel.MomentNotes boundingRectWithSize:CGSizeMake(kFullWidth-leftPading*2, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.width;

    
    
    CGFloat realyContentHeight = [dynamicModel.MomentNotes boundingRectWithSize:CGSizeMake(contentWidht, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.height+10;
    
    


    CGFloat showContentHeight = 0;

    
    
    
    if (!dynamicModel.MomentNotes.length){
        realyContentHeight = 10;
    }
    
    if (_style == HRFollowLayoutStyleDetail){
        self.isShowRealyCellHeight = YES;
    }
    
    if (self.isShowRealyCellHeight)
    {
        showContentHeight = realyContentHeight;
        self.isShowMoreContent = YES;
    }else{
        if (realyContentHeight > 150)
        {
            showContentHeight = 150;
            self.isShowMoreContent = YES;
        }else{
            showContentHeight = realyContentHeight;
            self.isShowMoreContent = NO;
        }
    }
    
    _contentLabelFrame = CGRectMake(leftPading, CGRectGetMaxY(_iconImageViewFrame), contentWidht, showContentHeight);


    
    
    
    if (self.isShowMoreContent){
        _showMoreContentButtonFrame = CGRectMake(leftPading, CGRectGetMaxY(_contentLabelFrame), 40, 30);

    }else{
        _showMoreContentButtonFrame = CGRectMake(leftPading, CGRectGetMaxY(_contentLabelFrame), 0, 0);
    }

    
    

    
    CGFloat topHeight = 0;

    
    // 动态的类型：1：语音+文字动态 2：图片+文字动态(可多图文) 文字可以为空 3：纯文字动态  4 视频贴
    if ([_dynamicModel.MomentType intValue] == 1){
        _soundButtonFrame = CGRectMake(leftPading, CGRectGetMaxY(_showMoreContentButtonFrame), 170, 50);
        
        topHeight = CGRectGetMaxY(_soundButtonFrame);
        
        

    }else if ([_dynamicModel.MomentType intValue] == 2){
        CGSize imageViewSize;
        
        if (dynamicModel.pics){
            imageViewSize = [HRFollowDynamicPhotoView sizePhotoViewWithPhotoCount:[dynamicModel.pics count] andToWidth:kFullWidth];
        }
        
        _dynamicPhotoViewFrame = CGRectMake(0, CGRectGetMaxY(_showMoreContentButtonFrame), imageViewSize.width, imageViewSize.height);
        
        topHeight = CGRectGetMaxY(_dynamicPhotoViewFrame);
    }else if ([_dynamicModel.MomentType intValue] == 3){
        topHeight = CGRectGetMaxY(_showMoreContentButtonFrame);

    }else if ([_dynamicModel.MomentType intValue] == 4){
        _playerViewFrame = CGRectMake(0, CGRectGetMaxY(_showMoreContentButtonFrame), kFullWidth, kFullWidth);
        
        topHeight = CGRectGetMaxY(_playerViewFrame);

    }else if ([_dynamicModel.MomentType intValue] == 5){
        _soundAndImageViewFrame = CGRectMake(0, CGRectGetMaxY(_showMoreContentButtonFrame), kFullWidth, 120);
        topHeight = CGRectGetMaxY(_soundAndImageViewFrame);
    }
    
    CGFloat tagWidth  = [dynamicModel.MomentTagName sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}].width+15;

    
    
    if (_style == HRFollowLayoutStyleDetail){
        

        
        _tagButtonFrame = CGRectMake(_iconImageViewFrame.origin.x, topHeight+5, tagWidth, 20);
        
        _dynamicToolViewFrame = CGRectMake(0, CGRectGetMaxY(_tagButtonFrame)+leftPading, kFullWidth, 20);

        
    }else{
        
        
        _tagButtonFrame = CGRectMake(leftPading, topHeight+5, tagWidth, 20);

        
        _commentViewFrame = CGRectMake(_iconImageViewFrame.origin.x,CGRectGetMaxY(_tagButtonFrame)+leftPading, kFullWidth, [HRDynamicCommentView sizeDynamicCommentViewCommnetCount:_dynamicModel.comentListArray.count].height);
        
        _dynamicToolViewFrame = CGRectMake(0, CGRectGetMaxY(_commentViewFrame)+leftPading, kFullWidth, 20);


    }
    


    
    
    
    
    
    _contViewFrame = CGRectMake(0, 0, kFullWidth, CGRectGetMaxY(_dynamicToolViewFrame)+leftPading);

    
    
    
    
    
    _cellHeight = CGRectGetMaxY(_contViewFrame)+5;
    
}




- (NSAttributedString *) attStringFromTextForChatting:(NSString *) aInputText textFont:(UIFont*)font
{
    if (!aInputText.length){
        aInputText = @"";
    }
    
    NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:aInputText];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:8];
    

    [string addAttribute:NSParagraphStyleAttributeName
                   value:paragraphStyle
                   range:NSMakeRange(0, string.length)];
    if (font) {
        [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
    }
    return string;
}


@end
